# Load all modules which where requested
foreach( M IN LISTS MODULES_LIST )
    request_island_module(${M})
endforeach()

get_global_var(GLOBAL_REQUESTED_MODULES_LIST REQUESTED_MODULES_LIST)

while(REQUESTED_MODULES_LIST)

	get_global_var(GLOBAL_REQUESTED_MODULES_LIST REQUESTED_MODULES_LIST)

    # append current name of the module to the list of modules
    append_to_global_var(GLOBAL_REQUESTED_MODULES_LIST "${MODULE_NAME}")

    # remove any duplicates from requested modules 
    remove_duplicates_from_global_var(GLOBAL_REQUESTED_MODULES_LIST)

	# get the currently loaded modules list
	get_global_var(GLOBAL_LOADED_MODULES_LIST LOADED_MODULES_LIST)
		
    # remove any modules from the requested list which are already in the loaded list
    remove_from_global_var(GLOBAL_REQUESTED_MODULES_LIST "${LOADED_MODULES_LIST}")

    get_global_var(GLOBAL_REQUESTED_MODULES_LIST REQUESTED_MODULES_LIST)
    message(STATUS "requested modules 1: ${REQUESTED_MODULES_LIST}")
    
    # Load all modules which where requested and which were not yet loaded
    foreach( M IN LISTS REQUESTED_MODULES_LIST )
		message(STATUS "requesting modules: ${M}")
        load_island_module(${M})
    endforeach()
    
	# get the currently loaded modules list
	get_global_var(GLOBAL_LOADED_MODULES_LIST LOADED_MODULES_LIST)

	message(STATUS "loaded modules list: ${LOADED_MODULES_LIST}")
    remove_from_global_var(GLOBAL_REQUESTED_MODULES_LIST "${LOADED_MODULES_LIST}")

    # repeat this until no more modules are requested.
    
    # remove any elements from requested modules which are now present in loaded modules

    get_global_var(GLOBAL_REQUESTED_MODULES_LIST REQUESTED_MODULES_LIST)
    message(STATUS "requested modules (atend): ${REQUESTED_MODULES_LIST}")


endwhile(REQUESTED_MODULES_LIST)

# print_current_includes()

# NOTE: provide paths for libraries which are used by modules, 
# but not available via system search paths
# these were set via PARENT_SCOPE from the plugin CMake files.
link_directories(${PLUGIN_LINK_DIRS})

add_executable (${PROJECT_NAME} ${SOURCES})

# add -D definitions so that source code will know whether modules are linked
# statically or dynamically
if (PLUGINS_DYNAMIC)
    target_compile_definitions(${PROJECT_NAME} PUBLIC "PLUGINS_DYNAMIC=1")
endif()

# we need to link in the dynamic linking library so we can use dynamic linking
target_link_libraries(${PROJECT_NAME} PRIVATE ${CMAKE_DL_LIBS})

target_link_libraries(${PROJECT_NAME} PRIVATE ${PLUGIN_LIBS_DEPENDENCIES})

if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU") 
    # this is only needed for the GNU linker - it will force the linker to check all libraries for symbols,
    # not just the ones that are specified before a given library. 
    # more modern (and faster) linkers such as lld (llvm) or mold won't need this.
   target_link_libraries(${PROJECT_NAME} PRIVATE "-Wl,--start-group" $CACHE{STATIC_LIBRARIES} "-Wl,--end-group" )
else()
    # link static libs
    target_link_libraries(${PROJECT_NAME} PRIVATE $CACHE{STATIC_LIBRARIES} )
endif()


message(STATUS "Static libraries : $CACHE{STATIC_LIBRARIES}")

get_global_var(GLOBAL_LOADED_MODULES_LIST LOADED_MODULES_LIST)
message(STATUS "Loaded modules   : ${LOADED_MODULES_LIST}")

# Libraries against which we dynamically link our main application:
message(STATUS "Libs dependencies: ${PLUGIN_LIBS_DEPENDENCIES}")

##################################################################################
# OPTIONAL - Use Vulkan Validation Layers compiled from source -
# so that we may debug the validation layers, too (how meta!)

set(COMPILE_VALIDATION_LAYERS OFF CACHE BOOL "Compile Validation Layers as a subproject (useful to debug validation layers)")

function(compile_validation_layers)
    # this allows us to debug debug layers -- how meta!!!
    unset( CMAKE_ARCHIVE_OUTPUT_DIRECTORY )
    unset( CMAKE_LIBRARY_OUTPUT_DIRECTORY )
    unset( CMAKE_RUNTIME_OUTPUT_DIRECTORY )
	# set (CMAKE_BUILD_TYPE Debug)
    set (VulkanRegistry_DIR /usr/local/share/vulkan/registry)
    add_subdirectory(3rdparty/src/Vulkan-ValidationLayers ${ISLAND_BASE_DIR}/3rdparty/src/Vulkan-ValidationLayers/build)
endfunction(compile_validation_layers)

if (COMPILE_VALIDATION_LAYERS)
    set (GLSLANG_INSTALL_DIR "/usr/local" CACHE STRING "")
    compile_validation_layers()
endif()
